import utime
from machine import ADC,Pin,I2C

ADDRESS=56
if __name__=="__main__":
    import IIC256
    IIC256.LCDInit()
    from machine import RTC
    rtc = RTC()
    i2c = I2C(1,scl=Pin(18), sda=Pin(19), freq=100000)
    i2c.scan()
    # i2c.writeto(ADDRESS,b"\xba")
    # utime.sleep_ms(100)
    i2c.writeto(ADDRESS,b"\xe1\x08\x00")
    utime.sleep_ms(100)
    i2c.writeto(ADDRESS,b"\xac\x33\x00")
    utime.sleep_ms(100)
    raw=i2c.readfrom(ADDRESS,7)
    _temp= ((raw[3] & 0xF) << 16) | (raw[4] << 8) | raw[5]
    _temp = ((_temp * 200.0) / 0x100000) - 50
    _humidity = (raw[1] << 12) | (raw[2] << 4) | (raw[3] >> 4)
    _humidity = (_humidity * 100) / 0x100000
    print(raw)
    print(_temp,_humidity)
    while 1:

        t=rtc.datetime()
        if(t[6]%3==0):
            i2c.writeto(ADDRESS,b"\xac\x33\x00")
            utime.sleep_ms(100)
            raw=i2c.readfrom(ADDRESS,7)
            _temp= ((raw[3] & 0xF) << 16) | (raw[4] << 8) | raw[5]
            _temp = ((_temp * 200.0) / 0x100000) - 50
            _humidity = (raw[1] << 12) | (raw[2] << 4) | (raw[3] >> 4)
            _humidity = (_humidity * 100) / 0x100000
            IIC256.print32("%.2fC  %.2f%%"%(_temp,_humidity),0,1)
            
            print("aht10:   %.2fC  %.2f%%"%(_temp,_humidity))

        print(t)
        IIC256.lprint(str(t[6]))
        # IIC256.lprint("%02d:%02d:%02d"%(h,m,s))
        # print("%02d:%02d:%02d"%(h,m,s))
        t=rtc.datetime()
        utime.sleep_us(1000000-t[7])   